package com.manage.function.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.manage.common.core.domain.entity.SysDept;
import com.manage.common.core.domain.entity.SysUser;
import com.manage.common.utils.SecurityUtils;
import com.manage.function.domain.FunctionSocialSecurity;
import com.manage.function.domain.vo.UserAndDept;
import com.manage.system.domain.SysUserRole;
import com.manage.system.mapper.SysDeptMapper;
import com.manage.system.mapper.SysUserMapper;
import com.manage.system.mapper.SysUserRoleMapper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.manage.function.mapper.FunctionSalaryMapper;
import com.manage.function.domain.FunctionSalary;
import com.manage.function.service.IFunctionSalaryService;

import javax.annotation.Resource;

/**
 * 薪资信息Service业务层处理
 *
 * @author jinghj
 * @date 2025-03-09
 */
@Service
public class FunctionSalaryServiceImpl implements IFunctionSalaryService
{
    @Resource
    private FunctionSalaryMapper functionSalaryMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询薪资信息
     *
     * @param id 薪资信息主键
     * @return 薪资信息
     */
    @Override
    public FunctionSalary selectFunctionSalaryById(Long id)
    {

        return functionSalaryMapper.selectFunctionSalaryById(id);
    }

    /**
     * 查询薪资信息列表
     *
     * @param functionSalary 薪资信息
     * @return 薪资信息
     */
    @Override
    public List<FunctionSalary> selectFunctionSalaryList(FunctionSalary functionSalary)
    {
        List<FunctionSalary> functionSalaries=null;
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserMapper.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        SysUserRole sysUserRole = sysUserRoleMapper.selectSysUserRoleByUserId(userId);
        if (sysUserRole.getRoleId()==1){
           functionSalaries= functionSalaryMapper.selectFunctionSalaryList(functionSalary);
        }else {
            functionSalary.setUserName(username);
            functionSalaries=functionSalaryMapper.selectFunctionSalaryList(functionSalary);
        }

        return functionSalaries;
    }

    /**
     * 新增薪资信息
     *
     * @param functionSalary 薪资信息
     * @return 结果
     */
    @Override
    public int insertFunctionSalary(FunctionSalary functionSalary)
    {
        return functionSalaryMapper.insertFunctionSalary(functionSalary);
    }

    /**
     * 修改薪资信息
     *
     * @param functionSalary 薪资信息
     * @return 结果
     */
    @Override
    public int updateFunctionSalary(FunctionSalary functionSalary)
    {
        return functionSalaryMapper.updateFunctionSalary(functionSalary);
    }

    /**
     * 批量删除薪资信息
     *
     * @param ids 需要删除的薪资信息主键
     * @return 结果
     */
    @Override
    public int deleteFunctionSalaryByIds(Long[] ids)
    {
        return functionSalaryMapper.deleteFunctionSalaryByIds(ids);
    }

    /**
     * 删除薪资信息信息
     *
     * @param id 薪资信息主键
     * @return 结果
     */
    @Override
    public int deleteFunctionSalaryById(Long id)
    {
        return functionSalaryMapper.deleteFunctionSalaryById(id);
    }

    @Override
    public List<UserAndDept> selectUserAndDept() {
        SysUser sysUser = new SysUser();
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        List<UserAndDept> userAndDepts = new ArrayList<>();
        for (SysUser sysUser1 : sysUsers) {
            UserAndDept userAndDept = new UserAndDept();
            userAndDept.setUserId(sysUser1.getUserId());
            userAndDept.setUserName(sysUser1.getUserName());
            userAndDept.setDeptId(sysUser1.getDeptId());

            // 检查 deptId 是否为空，避免调用 null 对象的方法
            if (sysUser1.getDeptId() != null) {
                SysDept sysDept = sysDeptMapper.selectDeptById(sysUser1.getDeptId());
                if (sysDept != null) {
                    userAndDept.setDeptName(sysDept.getDeptName());
                } else {
                    userAndDept.setDeptName("未知部门"); // 或者其他默认值
                }
            } else {
                userAndDept.setDeptName("无部门");
            }
            userAndDepts.add(userAndDept);

        }

        return userAndDepts;
    }
}
